<template>
  <div class="container">
    <CommentList v-if="openid" type="user" :comments="comments"></CommentList>
    <div v-if="openid">
      <div class="page-title">我的图书</div>
      <Card v-for="book in books" :key="book.id" :book="book"></Card>
      <div v-if="!books.length">暂时还没添加过书，快去添加几本吧</div>
    </div>

  </div>
</template>
<script>
import {get} from '@/util'
import CommentList from '@/components/CommentList'
import Card from '@/components/Card'
export default {
  data: function () {
    return {
      comments: [],
      books: [],
      openid: ''
    }
  },
  components: {
    CommentList, Card
  },
  methods: {
    init () {
      wx.showNavigationBarLoading()
      this.getComments()
      this.getBooks()
      wx.hideNavigationBarLoading()
    },
    async getBooks () {
      const books = await get('/weapp/booklist', {
        openid: this.openid
      })
      this.books = books.list
    },
    async getComments () {
      const comments = await get('/weapp/commentlist', {
        openid: this.openid
      })
      this.comments = comments.list
    }
  },
  onPullDownRefresh () {
    this.init()
    wx.stopPullDownRefresh()
  },
  onShow () {
    if (!this.openid) {
      let openid = wx.getStorageSync('openid')
      if (openid) {
        this.openid = openid
        this.init()
      }
    }
  }
}
</script>
<style>
</style>
